-- Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
-- 
-- this file licensed under the Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
--
-- THIS SOFTWARE IS PROVIDED ON AN \"AS IS\" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
-- PURPOSE.
-- See the Mulan PSL v2 for more details.

local class = require 'mc.class'
local client = require 'thermal_mgmt.client'
local enums = require 'basic_cooling.define.cooling_enums'
local props = require 'basic_cooling.define.cooling_properties'
local base = require 'basic_cooling.data_keeping.base'
local singleton = require 'mc.singleton'

local energy_metric_data_keeping = class(base)

function energy_metric_data_keeping:ctor(bus)
    self.obj_name = "EnergyMetric"
    -- 监听的资源树路径
    self.paths_table = {
        "/bmc/kepler/Systems/:SystemId/EnergyMetric"
    }
    self.interface = "bmc.kepler.Systems.EnergyMetric"
    -- 监听的接口
    self.interfaces_table = {
        [self.interface] = {
            [enums.data_keeping_config.FOREACH_FUNC] = client.ForeachEnergyMetricObjects,
            [enums.data_keeping_config.PROPERTIES] = {}
        }
    }
end

function energy_metric_data_keeping:init()
    self.super.init(self)
end

function energy_metric_data_keeping:action_after_obj_added(path, interface)
    self.path = path
end

function energy_metric_data_keeping:get_system_power()
    if not self.objs or not self.path or not self.objs[self.path] or not self.objs[self.path][self.interface] or
        not self.objs[self.path][self.interface].obj then
        return nil
    end
    return self.objs[self.path][self.interface].obj[props.SYSTEM_POWER]
end

function energy_metric_data_keeping:get_fan_total_power()
    if not self.objs or not self.path or not self.objs[self.path] or not self.objs[self.path][self.interface] or
        not self.objs[self.path][self.interface].obj then
        return nil
    end
    return self.objs[self.path][self.interface].obj[props.FAN_TOTAL_POWER_WATTS]
end

return singleton(energy_metric_data_keeping)